package a66weding.com.jiehuntong.activity;

import android.app.Dialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.adapter.MyDinghunAdapter;
import a66weding.com.jiehuntong.base.BaseActivity;
import a66weding.com.jiehuntong.beans.Diy_GoodsBean;
import a66weding.com.jiehuntong.common.utils.CUtils;
import a66weding.com.jiehuntong.date.NumericWheelAdapter;
import a66weding.com.jiehuntong.date.OnWheelScrollListener;
import a66weding.com.jiehuntong.date.WheelView;

/**
 * Created by admin on 2016/6/20.
 */
public class Diy_DinghunActivity extends BaseActivity implements View.OnClickListener {
    private List<Diy_GoodsBean> list;
    private ListView lv;
    private MyDinghunAdapter adapter;
    private ImageView ivBack;
    private Dialog setDialog;
    private TextView tvCancle, tvQueren;
    private RelativeLayout rl_dhdate, rl_txdate, rl;
    private LayoutInflater inflater = null;
    private WheelView year;
    private WheelView month;
    private WheelView day;
    private int mYear = 1996;
    private int mMonth = 0;
    private int mDay = 1;
    private String birthday;

    LinearLayout ll;
    TextView tv1, tv2;
    private View dateView;
    private TextView tv_dhdate, tv_txdate;
    private RelativeLayout rl_upload;

    @Override
    protected View getContentView() {
        View view = View.inflate(this, R.layout.activity_diy_dinghun, null);

        lv = (ListView) view.findViewById(R.id.diy_dinghun_listview);
        ivBack = (ImageView) view.findViewById(R.id.back_iv);
        rl_dhdate = (RelativeLayout) view.findViewById(R.id.dh_dhdate_rl);
        rl_txdate = (RelativeLayout) view.findViewById(R.id.dh_txdate_rl);
        tv_dhdate = (TextView) view.findViewById(R.id.textview_dhdate);
        tv_txdate = (TextView) view.findViewById(R.id.textview_txdate);
        rl_upload = (RelativeLayout) view.findViewById(R.id.dh_upload_rl);

        ivBack.setOnClickListener(this);
        rl_dhdate.setOnClickListener(this);
        rl_txdate.setOnClickListener(this);
        rl_upload.setOnClickListener(this);

        initData();
        return view;
    }

    private void initData() {
        String[] act = {"最热", "最热", "最热", "最热", "最热", "最热"};
        String[] cont = {"【爱丽丝】唯美高清婚礼酒店", "【爱丽丝】唯美高清婚礼酒店", "【爱丽丝】唯美高清婚礼酒店", "【爱丽丝】唯美高清婚礼酒店", "【爱丽丝】唯美高清婚礼酒店", "【爱丽丝】唯美高清婚礼酒店"};
        int[] icon = {R.mipmap.b, R.mipmap.c, R.mipmap.d, R.mipmap.e, R.mipmap.f, R.mipmap.g};
        String[] name = {"锦园大酒店", "锦园大酒店", "锦园大酒店", "锦园大酒店", "锦园大酒店", "锦园大酒店",};
        String[] costPri = {"8989", "8989", "8989", "8989", "8989", "8989"};
        String[] cutPri = {"6652", "6652", "6652", "6652", "6652", "6652"};
        list = new ArrayList<Diy_GoodsBean>();
        for (int i = 0; i < cont.length; i++) {
            Diy_GoodsBean object = new Diy_GoodsBean();
            object.hotAct = act[i];
            object.hotCont = cont[i];
            object.hotIcon = icon[i];
            object.hotName = name[i];
            object.costPri = costPri[i];
            object.cutPri = cutPri[i];
            list.add(object);
        }
        adapter = new MyDinghunAdapter(this, list);
        lv.setAdapter(adapter);

        //以下代码解决ScrollView嵌套listView只显示一行问题
        int totalHeight = 0;
        for (int j = 0, len = adapter.getCount(); j < len; j++) {
            // listAdapter.getCount()返回数据项的数目
            View listItem = adapter.getView(j, null, lv);
            // 计算子项View 的宽高
            listItem.measure(0, 0);
            // 统计所有子项的总高度
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = lv.getLayoutParams();
        params.height = totalHeight + (lv.getDividerHeight() * (adapter.getCount() - 1));
        // params.height最后得到整个ListView完整显示需要的高度
        lv.setLayoutParams(params);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dh_upload_rl:
                CUtils.showToast("ss");
                //上传照片
                Diy_HeadActivity dh = new Diy_HeadActivity();
                break;
            case R.id.back_iv:
                //返回
                finish();
                break;
            case R.id.dh_dhdate_rl:
                //订婚日期
                showDate(1);
                break;
            case R.id.dh_txdate_rl:
                //提醒日期
                showDate(2);
                break;
        }
    }

    private void showDate(final int i) {
        setDialog = new Dialog(this, R.style.dialog);
        setDialog.setContentView(R.layout.diy_dialog_date);
        setDialog.setCanceledOnTouchOutside(false);
        tvCancle = (TextView) setDialog.findViewById(R.id.diy_datecancel_tv);
        tvQueren = (TextView) setDialog.findViewById(R.id.diy_datequeren_tv);
        rl = (RelativeLayout) setDialog.findViewById(R.id.rl);
        inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
        rl.addView(getDataPick());

        tvCancle.setOnClickListener(this);
        tvQueren.setOnClickListener(this);
        tvCancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setDialog.dismiss();
            }
        });
        tvQueren.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断选择提醒或订婚日期
                if (i == 1) {
                    tv_dhdate.setText(birthday);
                } else if (i == 2) {
                    tv_txdate.setText(birthday);
                }
                setDialog.dismiss();
            }
        });
        Window window = setDialog.getWindow();
        // 可以在此设置显示动画
        WindowManager.LayoutParams wl = window.getAttributes();
        wl.x = 0;
        wl.y = getWindowManager().getDefaultDisplay().getHeight();
        // 以下这两句是为了保证按钮可以水平满屏
        wl.width = ViewGroup.LayoutParams.MATCH_PARENT;
        wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        // 设置显示位置
        setDialog.onWindowAttributesChanged(wl);
        setDialog.show();
    }

    /**
     * @return
     */
    private View getDataPick() {
        Calendar c = Calendar.getInstance();
        int norYear = c.get(Calendar.YEAR);

        int curYear = mYear;
        int curMonth = mMonth + 1;
        int curDate = mDay;

        dateView = inflater.inflate(R.layout.diy_dialog_date_picker, null);

        year = (WheelView) dateView.findViewById(R.id.year);
        NumericWheelAdapter numericWheelAdapter1 = new NumericWheelAdapter(this, 1900, norYear);
        numericWheelAdapter1.setLabel("年");
        year.setViewAdapter(numericWheelAdapter1);
        year.setCyclic(true);//是否可循环滑动
        year.addScrollingListener(scrollListener);

        month = (WheelView) dateView.findViewById(R.id.month);
        NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(this, 1, 12, "%02d");
        numericWheelAdapter2.setLabel("月");
        month.setViewAdapter(numericWheelAdapter2);
        month.setCyclic(true);
        month.addScrollingListener(scrollListener);

        day = (WheelView) dateView.findViewById(R.id.day);
        initDay(curYear, curMonth);
        day.setCyclic(true);

        year.setVisibleItems(7);//设置显示行数
        month.setVisibleItems(7);
        day.setVisibleItems(7);

        year.setCurrentItem(curYear - 1950);
        month.setCurrentItem(curMonth - 1);
        day.setCurrentItem(curDate - 1);
        return dateView;
    }

    OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {

        }

        @Override
        public void onScrollingFinished(WheelView wheel) {
            int n_year = year.getCurrentItem() + 1900;//年
            int n_month = month.getCurrentItem() + 1;//月

            initDay(n_year, n_month);

            birthday = new StringBuilder().append((year.getCurrentItem() + 1900)).append("-").append((month.getCurrentItem() + 1) < 10 ? "0" + (month.getCurrentItem() + 1) : (month.getCurrentItem() + 1)).append("-").append(((day.getCurrentItem() + 1) < 10) ? "0" + (day.getCurrentItem() + 1) : (day.getCurrentItem() + 1)).toString();

        }
    };

    /**
     * @param year
     * @param month
     * @return
     */
    private int getDay(int year, int month) {
        int day = 30;
        boolean flag = false;
        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }

    /**
     */
    private void initDay(int arg1, int arg2) {
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(this, 1, getDay(arg1, arg2), "%02d");
        numericWheelAdapter.setLabel("日");
        day.setViewAdapter(numericWheelAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
